//
//  NSDateExtesion.swift
//  Lottery
//
//  Created by 黄丹 on 2019/1/5.
//  Copyright © 2019 薛永伟. All rights reserved.
//

import Foundation
import YYKit

extension Date{
    
    /// YYYY-MM
    func MMdd() -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = "MM-dd"
        let str = formatter.string(from: self)
        return str
    }
    
    // YYYY-MM-dd HH:mm:ss
    func YYYYMMdd() -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = "YYYY-MM-dd"
        let str = formatter.string(from: self)
        return str
    }
    
    /// YYYY-MM-dd HH:mm:ss
    func YYYYMMddHHmmss() -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        let str = formatter.string(from: self)
        return str
    }
    
    /// YYYY-MM-dd HH:mm:ss
    func TodayHHmmss() -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        if self.isToday {
            formatter.dateFormat = "今天 HH:mm:ss"
        }
        let str = formatter.string(from: self)
        return str
    }
    
    
}

//MARK: - --------------判断--------------
extension Date{
    
    /// 是不是今天
    var isToday:Bool {
        
        if fabs(self.timeIntervalSinceNow) >= 60 * 60 * 24 {
            return false
        }
        
        let today = Calendar.current.dateComponents([.day], from: Date()).day
        
        let day = Calendar.current.dateComponents([.day], from: self).day
        
        return day == today
        
    }
    
}

extension Date {
    
    
    static func dateFrom(string:String,formater:String) -> Date? {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formater
        let timezone = TimeZone.current
        dateFormatter.timeZone = timezone
        
        let date = dateFormatter.date(from: string)
        return date
    }
}
